package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.SopHybridizationMapper;
import com.zhijian.medical.entity.po.SopHybridization;
import com.zhijian.medical.entity.vo.request.SopHybridizationSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.SopHybridizationSearchVo;
import com.zhijian.medical.entity.vo.response.SopHybridizationVo;
import com.zhijian.medical.service.SopHybridizationService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * SOP-荧光原位杂交(FISH)判读考核业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-15 09:49:30
 */
@Slf4j
@Service
public class SopHybridizationServiceImpl implements SopHybridizationService {

	@Autowired
    private SopHybridizationMapper mapper;

    @Override
    public SopHybridization selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public SopHybridizationVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(SopHybridization sopHybridization) {
        Date now = new Date();
        sopHybridization.setCreateTime(now);
        sopHybridization.setUpdateTime(now);
        return mapper.insert(sopHybridization) > 0 ? sopHybridization.getId() : 0L;
    }

    @Override
    public Long insertSelective(SopHybridization sopHybridization) {
        Date now = new Date();
        sopHybridization.setCreateTime(now);
        sopHybridization.setUpdateTime(now);
        return mapper.insertSelective(sopHybridization) > 0 ? sopHybridization.getId() : 0L;
    }

    @Override
    public boolean update(SopHybridization sopHybridization) {
        Date now = new Date();
        sopHybridization.setUpdateTime(now);
        return mapper.updateByPrimaryKey(sopHybridization) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(SopHybridization record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(SopHybridizationSaveOrUpdateVo saveOrUpdateVo) {
        SopHybridization sopHybridization = new SopHybridization();
        BeanUtils.copyProperties(saveOrUpdateVo, sopHybridization);
        return this.insert(sopHybridization) != 0;
    }

    @Override
    public boolean modify(SopHybridizationSaveOrUpdateVo saveOrUpdateVo) {
        SopHybridization old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        SopHybridization sopHybridization = new SopHybridization();
        BeanUtils.copyProperties(saveOrUpdateVo, sopHybridization);
        return this.update(sopHybridization);
    }

    @Override
    public SopHybridizationVo selectOne(SopHybridizationSearchVo sopHybridizationSearchVo) {
        return mapper.selectOne(sopHybridizationSearchVo);
     }

    @Override
    public Integer count(SopHybridizationSearchVo sopHybridizationSearchVo) {
        return mapper.count(sopHybridizationSearchVo);
    }

    @Override
    public List<SopHybridizationVo> selectList(SopHybridizationSearchVo sopHybridizationSearchVo) {
        return mapper.selectList(sopHybridizationSearchVo);
    }

    @Override
    public Page<SopHybridizationVo> selectPage(SopHybridizationSearchVo sopHybridizationSearchVo) {
        AssertUtil.check(Objects.isNull(sopHybridizationSearchVo.getPageNum()) || Objects.isNull(sopHybridizationSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(sopHybridizationSearchVo);
        //分页
        Page<SopHybridizationVo> page = new Page<>(true, count, sopHybridizationSearchVo.getPageNum(), sopHybridizationSearchVo.getPageSize());
        sopHybridizationSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<SopHybridizationVo> responseVos = this.selectList(sopHybridizationSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<SopHybridization> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(SopHybridizationSearchVo sopHybridizationSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(sopHybridizationSearchVo);
    }
    
}